<?php
use yii\helpers\Html;
$post=\Yii::$app->session->getFlash('post');
?>
<?php $this->beginBlock('title'); ?>
    <?php if(isset($model['id'])):?>编辑<?php else:?>新增<?php endif;?>广告
<?php $this->endBlock();?>
<?php $this->beginBlock('breadcrumb'); ?>
<li><a href="<?php echo \Yii::$app->urlManager->createUrl('/advertise/index')?>">广告管理</a></li>
<li class="active"><a href="<?php echo \Yii::$app->urlManager->createUrl('/advertise/edit',['id'=>isset($model['id'])?$model['id']:0]);?>"><?php if(isset($model['id'])):?>编辑<?php else:?>新增<?php endif;?>广告</a></li>
<?php $this->endBlock();?>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/css/fileinput.css" />
<style>
.show_upload { display:none; }
</style>
<div class="row">
	<div class="col-sm-12">
		<section class="panel">
			<div class="panel-body">
				<div class="form">
					<form class="cmxform form-horizontal tasi-form" method="post" action="<?php echo \Yii::$app->urlManager->createUrl('/advertise/save');?>">
						<div class="form-group">
                            <label for="title" class="control-label col-lg-2">标题</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="title" type="text" value="<?php if(isset($post['title'])){ echo $post['title'];}else{ echo Html::encode($model['title']?:''); }?>" name="title" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="url" class="control-label col-lg-2">地址</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="url" type="text" value="<?php if(isset($post['url'])){ echo $post['url'];}else{ echo Html::encode($model['url']?:''); }?>" name="url" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="number" class="control-label col-lg-2">图片</label>
                            <div class="col-lg-10">
                            	<?php if(isset($model['img_path'])):?>
	                            	<span class="file-input">
	                            		<div class="file-preview">
	                            			<div class="close fileinput-remove">×</div>
	                            			<div class="">
	                            				<div class="file-preview-thumbnails">
													<div data-fileindex="0" class="file-preview-frame">
	   													<img style="width:auto;height:160px;" alt="<?php echo isset($model['game_name'])?$model['game_name']:'';?>" title="<?php echo isset($model['game_name'])?$model['game_name']:'';?>" class="file-preview-image" src="<?=filepath($model['img_path']);?>">
	   													<div class="file-thumbnail-footer">
	    													<div style="width: 170px;" class="file-caption-name" title="<?php echo isset($model['game_name'])?$model['game_name']:'';?>"><?php echo isset($model['game_name'])?$model['game_name']:'';?></div>
														</div>
													</div>
												</div>
												<div class="clearfix"></div>
											</div>
										</div>
									</span>
									<div class="show_upload">
										<input id="file_img_path" class="file" type="file" name="file" multiple=true>
			                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
									</div>
                            	<?php else:?>
                            		<input id="file_img_path" class="file" type="file" name="file" multiple=true>
		                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
		                        <?php endif;?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="sort" class="control-label col-lg-2">排序值</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="sort" type="text" value="<?php if(isset($post['sort'])){ echo $post['sort'];}else{ echo Html::encode($model['sort']?:0); }?>" name="sort" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-10">
                                <input type="hidden" name="_csrf" id="_csrf" value="<?=\Yii::$app->request->csrfToken; ?>" />
        						<?php if(isset($model['id'])):?><input type="hidden" name="id" value="<?=$model['id'];?>" /><?php endif;?>
                                <input type="hidden" value="<?php echo isset($model['img_path'])?$model['img_path']:'';?>" id="img_path" name="img_path" />
                                <button class="btn btn-success" type="submit" id="savebtn"> 保存</button>
                                <button class="btn btn-default" type="button" onclick="javascript:history.go(-1)"> 取消</button>
                            </div>
                        </div>
                    </form>
                </div>
			</div>
		</section>
	</div>
</div>
<?php
$this->beginBlock('script');
?>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/js/fileinput.js"></script>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/summernote/dist/summernote.min.js"></script>
<script type="text/javascript">
//约束上传图片条件
$("#file_img_path").fileinput({
    allowedFileExtensions : ['jpg', 'png','gif'],
    showPreview : true,
    maxFileSize: 1000,
    minImageWidth: 500,
    minImageHeight: 500,
    showUpload:false,
    //maxFilesNum: 1,
});

$(function() {
	//开始上传图片
    $(document).on('change', "#file_img_path", function() {
    	var csrfToken = $('meta[name="csrf-token"]').attr("content");
        $.ajaxFileUpload({
            url : "<?php echo \Yii::$app->urlManager->createUrl('/storage/upload')?>",
            secureuri : false,
            fileElementId : 'file_img_path',
            dataType : 'json',
            data : { _csrf:csrfToken },
            success : function (data, status) {
                $("#img_path").val(data.path);
            },
            error : function (data, status, e) {
                alert(e);
            }
        });
    });
});

//修改图片时，关闭图片显示改为上传
$(".fileinput-remove").click(function(){
	$(".file-input").toggle();
	$("#img_path").val("");
	$(".show_upload").show();
	$(".file-input-new").show();
});
</script>
<?php $this->endBlock('script');?>